package org.jeecg.modules.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.api.vo.songs.MP3DetailVo;
import org.jeecg.modules.api.vo.lyric.LyricVo;
import org.jeecg.modules.api.vo.songs.SongsDetailVo;
import org.jeecg.modules.music.entity.MusListSong;
import org.jeecg.modules.music.entity.MusSong;
import org.jeecg.modules.music.entity.MusSongArtist;
import org.jeecg.modules.music.entity.dto.HomeMusCountDto;
import org.jeecg.modules.music.mapper.MusListSongMapper;
import org.jeecg.modules.music.mapper.MusSongArtistMapper;
import org.jeecg.modules.music.mapper.MusSongMapper;
import org.jeecg.modules.music.service.IMusSongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MusSongServiceImpl extends ServiceImpl<MusSongMapper, MusSong> implements IMusSongService {

    @Autowired
    private MusSongArtistMapper musSongArtistMapper;

    @Autowired
    private MusListSongMapper musListSongMapper;

    @Override
    public void saveSong(MusSong song) {
        this.save(song);
        QueryWrapper<MusSong> wrapper = new QueryWrapper<>();
        wrapper.eq("song_name",song.getSongName());
        MusSong musSong = baseMapper.selectOne(wrapper);
        if(musSong!=null){
            if(musSong.getArtistId()!=null){
                MusSongArtist songArtist = new MusSongArtist();
                songArtist.setSongId(musSong.getId());
                songArtist.setArtistId(musSong.getArtistId());
                musSongArtistMapper.insert(songArtist);
            }
            if(musSong.getAlbumId()!=null){
                MusListSong musListSong = new MusListSong();
                musListSong.setSongId(musSong.getId());
                musListSong.setSonglistId(musSong.getAlbumId());
                musListSongMapper.insert(musListSong);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSong(String songId) {
        MusSong song = baseMapper.selectById(songId);

        if(song!=null){
            if(song.getArtistId()!=null){
                musSongArtistMapper.deleteBySongId(songId);
            }
            if(song.getAlbumId()!=null){
                musListSongMapper.deleteBySongId(songId);;
            }
        }
        this.removeById(songId);
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatchSongs(String ids) {
        List<String> list = Arrays.stream(ids.split(",")).map(String::valueOf).collect(Collectors.toList());
        for (String songId : list){
            musSongArtistMapper.deleteBySongId(songId);
            musListSongMapper.deleteBySongId(songId);;
        }
        //1.删除用户
        this.removeByIds(Arrays.asList(ids.split(",")));
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editSong(MusSong musSong) {
        MusSong song = baseMapper.selectById(musSong.getId());
        if(song!=null){
            if(song.getArtistId()!=null){
                musSongArtistMapper.deleteListArtist(musSong.getId(),musSong.getArtistId());
            }
            if(song.getAlbumId()!=null){
                musListSongMapper.deleteListSong(musSong.getId(),musSong.getAlbumId());;
            }
        }
        if(musSong.getAlbumId()!=null){
            MusListSong musListSong = new MusListSong();
            musListSong.setSongId(musSong.getId());
            musListSong.setSonglistId(musSong.getAlbumId());
            musListSongMapper.insert(musListSong);
        }
        if(musSong.getArtistId()!=null){
            MusSongArtist songArtist = new MusSongArtist();
            songArtist.setSongId(musSong.getId());
            songArtist.setArtistId(musSong.getArtistId());
            musSongArtistMapper.insert(songArtist);
        }
        this.updateById(musSong);
    }

    @Override
    public List<SongsDetailVo> queryAPISongDetail(String[] ids) {
        return baseMapper.queryAPISongDetail(ids);
    }

    @Override
    public LyricVo queryLyric(String id) {
        return baseMapper.getLyric(id);
    }

    @Override
    public MP3DetailVo queryAudioFile(String songId) {
        return baseMapper.getMP3Url(songId);
    }

    @Override
    public List<SongsDetailVo> getSimilarSongs(String id,String singerId, String composer, String lyricist) {
        return baseMapper.querySimilarSongs(id,singerId,composer,lyricist);
    }

    @Override
    public List<SongsDetailVo> querySongsByAlbumId(String id) {
        return baseMapper.querySongsByAlbumId(id);
    }

    @Override
    public List<SongsDetailVo> getSongsListByAlbumId(String albumId) {
        return baseMapper.getSongsListByAlbumId(albumId);
    }

    @Override
    public List<SongsDetailVo> querySongsByArtistId(String id) {
        return baseMapper.getSongsByArtistId(id);
    }

    @Override
    public List<SongsDetailVo> searchSuggestSongs(String keywords) {
        return baseMapper.searchSuggestSongs(keywords);
    }

    @Override
    public List<SongsDetailVo> searchSongs(String keywords, Integer limit, Integer offset) {
        return baseMapper.searchSongs(keywords,limit,offset);
    }

    @Override
    public List<SongsDetailVo> querySongsByUserId(String uid) {
        return baseMapper.querySongsByUserId(uid);
    }

    @Override
    public List<HomeMusCountDto> queryGroupLanguage() {
        return baseMapper.queryGroupLanguage();
    }

    @Override
    public List<HomeMusCountDto> queryGroupGenre() {
        return baseMapper.queryGroupGenre();
    }


}
